#region "DictionaryCollection"

/*
 * ####     Developer Name : Wang Yucai
 * 
 * ####     Development Tool : Microsoft VisualStudio 2010 Ultimate Edition
 * 
 * ####     Create Time : 2014-07-14 11:26:12
 * 
 * ####     Namespace : Masterduner.SourceStudio.Practices.DevelopmentLibrary.Collections
 * 
 * ####     Type Name : DictionaryCollection
 * 
 * ####     Full Name : Masterduner.SourceStudio.Practices.DevelopmentLibrary.Collections.DictionaryCollection
 * 
 * ####     Machine Name : GLCHQWYCWINW7
 * 
 * ####     UserName : GUOCOLAND/wangyucai
 * 
 * ####     CLR Version : 4.0.30319.18444
 * 
 * ####     Target Framework Version : 4.0
 */

#endregion

using System;
using System.Collections.Generic;

namespace Masterduner.SourceStudio.Practices.DevelopmentLibrary.Collections
{
    /// <summary>
    /// <para>Masterduner.SourceStudio.Practices.DevelopmentLibrary.Collections.DictionaryCollection</para>
    /// <para>
    /// 重新封装的字典集合。
    /// </para>
    /// </summary>
    /// <typeparam name="TKey">键类型。</typeparam>
    /// <typeparam name="TValue">值类型。</typeparam>
    /// <remarks>
    /// <para>Target Framework Version : 4.0</para>
    /// </remarks>
    [Serializable]
    public class DictionaryCollection<TKey, TValue> : IDictionary<TKey, TValue>
    {
        private Dictionary<TKey, TValue> _baseCollection;
        private bool _readOnly;

        #region BaseCollection
        /// <summary>
        /// 设置或获取基础字典集合。
        /// </summary>
        protected virtual Dictionary<TKey, TValue> BaseCollection
        {
            get { return _baseCollection; }
            set { _baseCollection = value; }
        }
        #endregion

        #region Constructor

        /// <summary>
        /// <para>构造函数：</para>
        /// <para>初始化一个<see cref="DictionaryCollection{TKey,TValue}" />对象实例。</para>
        /// </summary>
        public DictionaryCollection()
        {
            this.BaseCollection = new Dictionary<TKey, TValue>();
            this.ReadOnly = false;
        }

        #endregion

        #region Add
        /// <summary>
        /// 添加键、值到此字典中。
        /// </summary>
        /// <param name="key">键。</param>
        /// <param name="value">值。</param>
        protected virtual void BaseAdd(TKey key, TValue value)
        {
            this.BaseCollection.Add(key, value);
        }

        /// <summary>
        /// 添加键值对到此字典集合中
        /// </summary>
        /// <param name="kvp"><see cref="KeyValuePairWrapper{TKey,TValue}"/>对象实例。</param>
        public virtual void Add(KeyValuePairWrapper<TKey, TValue> kvp)
        {
            this.BaseAdd(kvp.Key, kvp.Value);
        }

        /// <summary>
        /// 添加键、值到此字典中。
        /// </summary>
        /// <param name="key">键。</param>
        /// <param name="value">值。</param>
        public void Add(TKey key, TValue value)
        {
            this.BaseAdd(key, value);
        }
        #endregion

        #region ContainsKey
        /// <summary>
        /// 验证集合中是否包含键为<paramref name="key"/>的值。
        /// </summary>
        /// <param name="key">键。</param>
        /// <returns>如果包含则返回true；否则返回false。</returns>
        protected virtual bool BaseContainsKey(TKey key)
        {
            return this.BaseCollection.ContainsKey(key);
        }

        /// <summary>
        /// 验证集合中是否包含键为<paramref name="key"/>的值。
        /// </summary>
        /// <param name="key">键。</param>
        /// <returns>如果包含则返回true；否则返回false。</returns>
        public bool ContainsKey(TKey key)
        {
            return this.BaseContainsKey(key);
        }
        #endregion

        #region ContainsValue
        /// <summary>
        /// 验证集合中是否包含指定值。
        /// </summary>
        /// <param name="value">值。</param>
        /// <returns>如果包含则返回true；否则返回false。</returns>
        public virtual bool ContainsValue(TValue value)
        {
            return this.BaseCollection.ContainsValue(value);
        }
        #endregion

        #region Keys
        /// <summary>
        /// 获取键集合。
        /// </summary>
        public ICollection<TKey> Keys
        {
            get { return this.BaseCollection.Keys; }
        }
        #endregion

        #region Remove
        /// <summary>
        /// 从字典中移除键为<paramref name="key"/>的值。
        /// </summary>
        /// <param name="key">键。</param>
        /// <returns>如果移除成功则返回true；否则返回false。</returns>
        protected virtual bool BaseRemove(TKey key)
        {
            return this.BaseCollection.Remove(key);
        }

        /// <summary>
        /// 从字典中移除键为<paramref name="key"/>的值。
        /// </summary>
        /// <param name="key">键。</param>
        /// <returns>如果移除成功则返回true；否则返回false。</returns>
        public bool Remove(TKey key)
        {
            return this.BaseRemove(key);
        }
        #endregion

        #region TryGetValue
        /// <summary>
        /// 尝试获取键为<paramref name="key"/>的值。
        /// </summary>
        /// <param name="key">键。</param>
        /// <param name="value">值。</param>
        /// <returns>如果字典中包含键<paramref name="key"/>，则返回true；否则返回false。</returns>
        public virtual bool TryGetValue(TKey key, out TValue value)
        {
            return this.BaseCollection.TryGetValue(key, out value);
        }
        #endregion

        #region Values
        /// <summary>
        /// 获取字典的值集合。
        /// </summary>
        public virtual ICollection<TValue> Values
        {
            get { return this.BaseCollection.Values; }
        }
        #endregion

        #region BaseGet
        /// <summary>
        /// 获取键为<paramref name="key"/>的值。
        /// </summary>
        /// <param name="key">键。</param>
        /// <returns>值。</returns>
        protected virtual TValue BaseGet(TKey key)
        {
            return this.BaseCollection[key];
        }
        #endregion

        #region BaseSet
        /// <summary>
        /// 设置键为<paramref name="key"/>的值。
        /// </summary>
        /// <param name="key">键。</param>
        /// <param name="value">值。</param>
        protected void BaseSet(TKey key, TValue value)
        {
            this.BaseCollection[key] = value;
        }
        #endregion

        #region this
        /// <summary>
        /// 设置或获取键为<paramref name="key"/>的值。
        /// </summary>
        /// <param name="key">键。</param>
        /// <returns>值。</returns>
        public TValue this[TKey key]
        {
            get
            {
                return this.BaseGet(key);
            }
            set
            {
                this.BaseSet(key, value);
            }
        }
        #endregion

        #region Add
        /// <summary>
        /// 添加键值对到字典中。
        /// </summary>
        /// <param name="kvp"><see cref="KeyValuePair{TKey,TValue}"/>对象实例。</param>
        public void Add(KeyValuePair<TKey, TValue> kvp)
        {
            this.BaseAdd(kvp.Key, kvp.Value);
        }
        #endregion

        #region Clear
        /// <summary>
        /// 清空字典集合。
        /// </summary>
        protected virtual void BaseClear()
        {
            this.BaseCollection.Clear();
        }

        /// <summary>
        /// 清空字典集合。
        /// </summary>
        public void Clear()
        {
            this.BaseClear();
        }
        #endregion

        #region Contains
        /// <summary>
        /// 验证字典中是否包含指定键值对。
        /// </summary>
        /// <param name="kvp"><see cref="KeyValuePair{TKey,TValue}"/>对象实例。</param>
        /// <returns>如果包含则返回true；否则返回false。</returns>
        protected bool BaseContains(KeyValuePair<TKey, TValue> kvp)
        {
            return this.BaseCollection.ForceTo<ICollection<KeyValuePair<TKey, TValue>>>().Contains(kvp);
        }

        /// <summary>
        /// 验证字典中是否包含指定键值对。
        /// </summary>
        /// <param name="kvp"><see cref="KeyValuePair{TKey,TValue}"/>对象实例。</param>
        /// <returns>如果包含则返回true；否则返回false。</returns>
        public bool Contains(KeyValuePair<TKey, TValue> kvp)
        {
            return this.BaseContains(kvp);
        }
        #endregion

        #region CopyTo
        /// <summary>
        /// 尚未实现此方法。
        /// </summary>
        /// <param name="array"></param>
        /// <param name="arrayIndex"></param>
        [Obsolete]
        public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }
        #endregion

        #region Count
        /// <summary>
        /// 验证此字典集合的元素个数。
        /// </summary>
        public virtual int Count
        {
            get { return this.BaseCollection.Count; }
        }
        #endregion

        #region IsReadOnly
        /// <summary>
        /// 设置或获取此集合是否只读。
        /// </summary>
        protected virtual bool ReadOnly
        {
            get { return _readOnly; }
            set { _readOnly = value; }
        }

        /// <summary>
        /// 设置或获取此集合是否只读。
        /// </summary>
        public bool IsReadOnly
        {
            get { return this.ReadOnly; }
        }
        #endregion

        #region Remove
        /// <summary>
        /// 从此集合中移除指定的键值对。
        /// </summary>
        /// <param name="kvp"><see cref="KeyValuePair{TKey,TValue}"/>对象实例。</param>
        /// <returns>如果成功移除，则返回true；否则返回false。</returns>
        public bool Remove(KeyValuePair<TKey, TValue> kvp)
        {
            return this.BaseRemove(kvp.Key);
        }
        #endregion

        #region RemoveMatches
        /// <summary>
        /// 从集合中移除匹配的键值对。
        /// </summary>
        /// <param name="match">匹配条件。</param>
        public virtual void RemoveMatches(RemoveMatch<KeyValuePairWrapper<TKey, TValue>> match)
        {
            foreach (TKey key in this.Keys)
            {
                if (match(new KeyValuePairWrapper<TKey, TValue>(key, this[key])))
                    this.BaseRemove(key);
            }
        }

        /// <summary>
        /// 从集合中移除匹配的键值对。
        /// </summary>
        /// <param name="match">匹配条件。</param>
        public virtual void RemoveMatches(RemoveMatch<KeyValuePair<TKey, TValue>> match)
        {
            foreach (TKey key in this.Keys)
            {
                if (match(new KeyValuePair<TKey, TValue>(key, this[key])))
                    this.BaseRemove(key);
            }
        }
        #endregion

        #region GetEnumerator
        /// <summary>
        /// 获取此集合的<see cref="IEnumerator{T}"/>对象实例。
        /// </summary>
        /// <returns>继承自<see cref="IEnumerator{T}"/>接口的对象实例。</returns>
        public virtual IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
        {
            return this.BaseCollection.GetEnumerator();
        }
        #endregion

        #region System.Collections.IEnumerable.GetEnumerator
        /// <summary>
        /// 获取此集合的<see cref="System.Collections.IEnumerator"/>对象实例。
        /// </summary>
        /// <returns>继承自<see cref="System.Collections.IEnumerator"/>接口的对象实例。</returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.BaseCollection.GetEnumerator();
        }
        #endregion
    }
}

#region README

/*
 * ####     ©2014 Wang Yucai
 * 
 * ####     Contact me
 *                  
 *                  E-mail : Masterduner@Yeah.net
 *                  Mobile : 13621384677
 *                  QQ : 180261899
 */

#endregion